package org.xawl.news.dao.impl.news;

import java.util.List;

import org.xawl.news.dao.news.NewsDao;
import org.xawl.news.entity.news.News;
import org.xawl.news.util.DBUtil;

public class NewsDaoImpl implements NewsDao {

	@Override
	public List<News> findList() {
		String sql = "select * from t_news";
		List<News> news = DBUtil.getList(News.class, sql);
		return news;
	}

	@Override
	public News findById(int id) {
		String sql = "select * from t_news where id=?";
		News news = DBUtil.getObject(News.class, sql, id);
		return news;
	}

	@Override
	public boolean save(News news) {
		String sql = "insert into t_news(title, content, author, state, create_time, update_time) values(?,?,?,?,?,?)";
		Object[] params = {news.getTitle(), news.getContent(), news.getAuthor(), news.getState(), news.getCreateTime(), news.getUpdateTime()};
		boolean flag = DBUtil.executeUpdate(sql, params);
		return flag;
	}

	@Override
	public boolean delete(int id) {
		String sql = "delete from t_news where id=?";
		boolean flag = DBUtil.executeUpdate(sql, id);
		return flag;
	}

	@Override
	public boolean update(News news) {
		String sql = "update t_news set title=?, content=?, author=?, state=?, create_time=?, update_time=? where id=?";
		Object[] params = {news.getTitle(), news.getContent(), news.getAuthor(), news.getState(), news.getCreateTime(), news.getUpdateTime(), news.getId()};
		boolean flag = DBUtil.executeUpdate(sql, params);
		return flag;
	}

	@Override
	public int findCount() {
		String sql = "select count(*) from t_news";
		int count = DBUtil.getCount(sql);
		return count;
	}

}
